package com.chapp.lastFmWs.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.ElementListener;
import android.util.Log;

import com.chapp.base.Location;
import com.chapp.base.Venue;
import com.chapp.lastFmWs.parser.base.SearchBaseParser;

public class VenueSearchParser extends SearchBaseParser<Venue> {
	private final String tag = getClass().getName();

	private Element elementVenue = elementResults.getChild("venuematches")
			.getChild("venue");

	// private Element elementLocation = elementVenue.getChild("location");

	// <geo:point>
	// private Element elementGeo = elementVenue.getChild(geoNamesace, "point");

	private final Venue venue = new Venue();
	private final Location location = new Location();

	@Override
	protected void createListeners() {
		super.createListeners();

		// <venue>
		elementVenue.setElementListener(new ElementListener() {
			@Override
			public void end() {
				venue.setLocation(location.copy());
				response.addSearchedItem(venue.copy());
			}

			@Override
			public void start(Attributes attributes) {
				// venue = new Venue();
				// location = new Location();
				venue.clean();
				location.clean();
			}
		});

		UtilParser.venueListener(elementVenue, venue, location);
	}

	@Override
	public SearchResponse<Venue> parse(String xml) {
		try {
			parseXml(xml);
		} catch (SAXException e) {
			Log.e(xml, "Error", e);
			return null;
		}
		if (!statusOk) {
			response.setError(error);
		}
		return response;
	}
}
